<template>
	<view class="page">
		<image class="logo" :src="logoObj.logo_other"></image>
		<view class="box">
			  <u--input
			    :placeholder="loginType === 1 ? '用户名/手机号' : '手机号'"
			    border="surround"
			    v-model="input1"
			  ></u--input>
			  <u-gap height="40"></u-gap>
			<u--input
			  :placeholder="loginType === 1 ? '密码' : '验证码'"
			  border="surround"
			  v-model="input2"
			>
			<template slot="suffix" v-if="loginType === 2">
				<u-code
					ref="uCode"
					@change="codeChange"
					seconds="60"
					changeText="X秒重新获取"
				></u-code>
				<u-button
					:disabled="input1.length === 11 ? false : true"
					@tap="getCode"
					:text="tips"
					color="#FC2422"
					type="error"
					size="mini"
					:customStyle="{
						paddingLeft: '10rpx',
						paddingRight: '10rpx',
						height: '60rpx'
					}"
					></u-button>
				</template>
			</u--input>
			<u-gap height="80"></u-gap>
			<u-button text="登录" color="#FC2422" @tap="onSubmit"></u-button>
			<u-gap height="40"></u-gap>
			<view class="login-type" @tap="cutLoginType">{{loginType === 1 ? '验证码快捷登录' : '密码登录'}}</view>
			<u-gap height="60"></u-gap>
			<view class="other-login">
				<image class="wx-image" src="@/static/images/wx.png"></image>
				<u-gap height="30"></u-gap>
				<view class="other-btn">
					<text class="other-btn_text">找回密码</text>
					<u-line direction="col" color="#999999" length="20rpx" margin="20rpx"></u-line>
					<text class="other-btn_text">注册账号</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {getLoginApi} from '@/api/user.js'
	export default {
		data() {
			return {
				logoObj: {},
				loginType: 1, // 1密码登录2手机号登录
				tips: '',
				input1: '',
				input2: ''
			};
		},
		onLoad({logo}) {
			this.getLoginData()
		},
		methods: {
			async onSubmit() {
				if(this.loginType === 1) {
					// 判空
					if(this.input1 === '' || this.input1.length === 0) {
						uni.showToast({
							title: '用户名/手机号不能为空',
							icon: 'none'
						})
						return
					}
					if(this.input2 === '' || this.input2.length === 0) {
						uni.showToast({
							title: '密码不能为空',
							icon: 'none'
						})
						return
					}
					if(this.input2.length < 6) {
						uni.showToast({
							title: '密码需大于等于6位数',
							icon: 'none'
						})
						return
					}
					// 账号密码登录
					this.$store.dispatch('userNameLogin',{
						username: this.input1,
						password: this.input2
					})
				}else {
					// 验证码登录
				}
			},
			codeChange(text) {
				this.tips = text;
			},
			getCode() {
				console.log(this.$refs.uCode.canGetCode)
				this.$refs.uCode.start();
			},
			cutLoginType() {
				this.loginType = this.loginType === 1 ? 2 : 1
			},
			async getLoginData() {
				const res = await getLoginApi()
				this.logoObj = res.data
			}
		}
	}
</script>

<style lang="scss" scoped>
.page {
	padding: 0 30rpx;
	box-sizing: border-box;
	display: flex;
	justify-content: center;
	align-items: center;
	flex-direction: column;
	height: calc(100vh - 44px);
	.logo {
		width: 400rpx;
		height: 200rpx;
	}
	.box {
		margin-top: 20rpx;
		width: 100%;
		.login-type {
			font-size: 28rpx;
			color: #333333;
			text-align: center;
		}
		.other-login {
			display: flex;
			justify-content: center;
			align-items: center;
			flex-direction: column;
			.wx-image {
				width: 80rpx;
				height: 80rpx;
			}
			.other-btn {
				display: flex;
				align-items: center;
				&_text {
					font-size: 28rpx;
					color: #666666;
				}
			}
		}
	}
}
</style>
